/*********************************************************
Copyright:

Copyright (c) 2007, Li Jie (qiezi) cpunion@gmail.com

All rights reserved.<br />

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:<br />

Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer. <br />
Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution. <br />
Neither the name of the <ORGANIZATION> nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission. <br />

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Authors: Li Jie (qiezi) cpunion@gmail.com
*********************************************************/


module bdb.sequence;


private import bdb.c.db;
private import bdb.exception;
private import bdb.database;
private import bdb.transaction;
private import std.c.stdlib;


class Sequence
{
public:
	enum CreateFlag {
		Default				= 0
	}

	enum OpenFlag {
		Default				= 0,
		Create				= DB_CREATE,
		Excl				= DB_EXCL,
		Thread				= DB_THREAD
	}

	enum CloseFlag {
		Default				= 0
	}

	enum GetFlag {
		Default				= 0,
		TransactionNoSync	= DB_TXN_NOSYNC
	}

	enum Flag {
		Decrement			= DB_SEQ_DEC,
		Increment			= DB_SEQ_INC,
		Wrap				= DB_SEQ_WRAP
	}

	enum RemoveFlag {
		Default				= 0,
		TransactionNoSync	= DB_TXN_NOSYNC
	}

	this(Database db, CreateFlag flags = CreateFlag.Default)
	{
		int ret = db_sequence_create(&seq, db._db(), cast(u_int32_t)flags);
		checkBdbError("create sequence", flags);
		closed = true;
	}

	~this() {
		close();
	}

	void open(Transaction txn, ubyte[] key, OpenFlag flags = OpenFlag.Default)
	{
		DB_TXN* _txn = null;
		if (txn) _txn = txn._txn();
		DBT _key;
		_key.flags = DB_DBT_USERMEM;
		_key.data = key.ptr;
		_key.size = key.length;

		int ret = seq.open(seq, _txn, &_key, cast(u_int32_t)flags);
		checkBdbError("open sequence", ret);
		closed = false;
	}

	void close(CloseFlag flags = CloseFlag.Default)
	{
		if (!closed) {
			int ret = seq.close(seq, cast(u_int32_t)flags);
			checkBdbError("close sequence", ret);
		}
	}

	void initial_value(db_seq_t value)
	{
		int ret = seq.initial_value(seq, value);
		checkBdbError("set sequence initial value", ret);
	}

	db_seq_t get(Transaction txn, int32_t delta, GetFlag flags = GetFlag.Default)
	{
		DB_TXN* _txn = null;
		if (txn) _txn = txn._txn();
		db_seq_t key;

		int ret = seq.get(seq, _txn, delta, &key, cast(u_int32_t)flags);
		checkBdbError("get sequence next value", ret);
		return key;
	}

	char[] key()
	{
		DBT key;
		key.flags = DB_DBT_MALLOC;
		int ret = seq.get_key(seq, &key);
		checkBdbError("get sequence key", ret);

		char[] result = (cast(char*)key.data)[0..key.size].dup;
		free(key.data);
		return result;
	}

	void remove(Transaction txn, RemoveFlag flags = RemoveFlag.Default)
	{
		DB_TXN* _txn = null;
		if (txn) _txn = txn._txn();

		int ret = seq.remove(seq, _txn, cast(u_int32_t)flags);
		checkBdbError("remove sequence", ret);
	}

	int32_t cachesize()
	{
		int32_t size;
		int ret = seq.get_cachesize(seq, &size);
		checkBdbError("get cachesize", ret);
		return size;
	}

	void cachesize(int32_t size)
	{
		int ret = seq.set_cachesize(seq, size);
		checkBdbError("set cachesize", ret);
	}

	Flag flags()
	{
		u_int32_t f;
		int ret = seq.get_flags(seq, &f);
		checkBdbError("get flags", ret);
		return cast(Flag)f;
	}

	void flags(Flag f)
	{
		int ret = seq.set_flags(seq, cast(u_int32_t)f);
		checkBdbError("set flags", ret);
	}

	void set_range(db_seq_t min, db_seq_t max)
	{
		int ret = seq.set_range(seq, min, max);
		checkBdbError("set range", ret);
	}

	void get_range(out db_seq_t min, out db_seq_t max)
	{
		int ret = seq.get_range(seq, &min, &max);
		checkBdbError("get range", ret);
	}

private:
	DB_SEQUENCE* seq;
	bool closed;
}